package ai.people.netmon.framework.domain.archives.entity;

import ai.people.netmon.framework.domain.archives.vo.ArchivesAddVO;
import ai.people.netmon.framework.domain.archives.vo.ArchivesVO;
import ai.people.netmon.utils.StringPool;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * 基本信息
 *
 * @author zhenglin
 * @since 2022-03-30
 */
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@TableName(schema = "webservice", value = "person_archives_base_info")
public class ArchivesBaseInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 主键ID
     */
    @TableId
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long id;

    /**
     * 姓名
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String name;

    /**
     * 性别 0:未知, 1:男 ,2:女
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private Integer gender;

    /**
     * 照片
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String picture;

    /**
     * 出生日期
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String birthday;

    /**
     * 体重
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private Double weight;

    /**
     * 身高
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private Double height;

    /**
     * 籍贯
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String hometown;

    /**
     * 现居地
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String residentialAddress;

    /**
     * 爱好
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String hobby;

    /**
     * 身份证
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String idNumber;
    /**
     * 护照
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String passport;

    /**
     * 手机号
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String phoneNumber;

    /**
     * 车牌号
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String plateNumber;

    /**
     * 标签
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String tags;

    /**
     * 银行账号
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String bankAccount;

    /**
     * 教育经历
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String educationExperience;

    /**
     * 就业经历
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String workExperience;

    /**
     * 政治立场
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String political;

    /**
     * 家庭信息
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String familySituation;

    /**
     * 个人信息
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String personalSituation;

    /**
     * 档案研判
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String archivesJudgment;

    /**
     * 备注
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String note;

    /**
     * 创建人ID
     */
    private Long creatorId;
    /**
     * 创建人姓名
     */
    private String creatorName;

    /**
     * 创建时间
     */
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    private LocalDateTime updateTime;

    /**
     * 坐标轴
     */
    private String coordinateAxis;

    public List<String> buildName() {
        if (StringUtils.isNotBlank(this.name)) {
            return List.of(this.name.split(StringPool.COMMA));
        }
        return Collections.emptyList();
    }

    public List<String> buildPhone() {
        if (StringUtils.isNotBlank(this.phoneNumber)) {
            return List.of(this.phoneNumber.split(StringPool.COMMA));
        }
        return Collections.emptyList();
    }

    public List<String> buildPlate() {
        if (StringUtils.isNotBlank(this.plateNumber)) {
            return List.of(this.plateNumber.split(StringPool.COMMA));
        }
        return Collections.emptyList();
    }

    public List<String> buildTags() {
        if (StringUtils.isNotBlank(this.tags)) {
            return List.of(this.tags.split(StringPool.COMMA));
        }
        return Collections.emptyList();
    }

    public List<ArchivesVO.Bank> buildBankAccount() {
        if (StringUtils.isNotBlank(this.bankAccount)) {
            return JSONArray.parseArray(this.bankAccount, ArchivesVO.Bank.class);
        }
        return Collections.emptyList();
    }

    public List<ArchivesVO.Education> buildEducationExperience() {
        if (StringUtils.isNotBlank(this.educationExperience)) {
            return JSONArray.parseArray(this.educationExperience, ArchivesVO.Education.class);
        }
        return Collections.emptyList();
    }

    public List<ArchivesVO.Work> buildWorkExperience() {
        if (StringUtils.isNotBlank(this.workExperience)) {
            return JSONArray.parseArray(this.workExperience, ArchivesVO.Work.class);
        }
        return Collections.emptyList();
    }


    public ArchivesBaseInfo(ArchivesVO archivesVO) {
        this.id = archivesVO.getId();
        this.name = archivesVO.namesStr();
        this.gender = archivesVO.getGender();
        this.picture = archivesVO.getPicture();
        this.birthday = archivesVO.getBirthday();
        this.weight = archivesVO.getWeight();
        this.height = archivesVO.getHeight();
        this.residentialAddress = archivesVO.getResidentialAddress();
        this.hobby = archivesVO.getHobby();
        this.idNumber = archivesVO.getIdNumber();
        this.passport = archivesVO.getPassport();
        this.phoneNumber = archivesVO.phoneNumbersStr();
        this.plateNumber = archivesVO.plateNumbersStr();
        this.tags = archivesVO.tagsStr();
        this.bankAccount = archivesVO.bankAccountsJsonStr();
        this.educationExperience = archivesVO.educationExperiencesJsonStr();
        this.workExperience = archivesVO.workExperiencesJsonStr();
        this.political = archivesVO.getPolitical();
        this.familySituation = archivesVO.getFamilySituation();
        this.personalSituation = archivesVO.getPersonalSituation();
        this.archivesJudgment = archivesVO.getArchivesJudgment();
        this.note = archivesVO.getNote();
        this.hometown = archivesVO.getHometown();
    }

    public ArchivesBaseInfo(ArchivesAddVO archivesVO) {
        this.name = archivesVO.namesStr();
        this.gender = archivesVO.getGender();
        this.picture = archivesVO.getPicture();
        this.birthday = archivesVO.getBirthday();
        this.weight = archivesVO.getWeight();
        this.height = archivesVO.getHeight();
        this.residentialAddress = archivesVO.getResidentialAddress();
        this.hobby = archivesVO.getHobby();
        this.idNumber = archivesVO.getIdNumber();
        this.passport = archivesVO.getPassport();
        this.phoneNumber = archivesVO.phoneNumbersStr();
        this.plateNumber = archivesVO.plateNumbersStr();
        this.tags = archivesVO.tagsStr();
        this.bankAccount = archivesVO.bankAccountsJson();
        this.educationExperience = archivesVO.educationExperiencesJson();
        this.workExperience = archivesVO.workExperiencesJson();
        this.political = archivesVO.getPolitical();
        this.familySituation = archivesVO.getFamilySituation();
        this.personalSituation = archivesVO.getPersonalSituation();
        this.archivesJudgment = archivesVO.getArchivesJudgment();
        this.note = archivesVO.getNote();
        this.hometown = archivesVO.getHometown();
    }


    public String buildNameFirst() {
        if (StringUtils.isNotBlank(this.name)) {
            String[] split = this.name.split(StringPool.COMMA);
            if (split.length > 0) {
                return split[0];
            }
        }
        return null;
    }
}
